package com.fxkj.cloud.drms.domain.device.repository;

import com.fxkj.cloud.dbs.common.constants.DeviceStatusEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>description: </p>
 * @author ww
 * @date 2025/4/10 17:19
 */
@Getter
public class ProductDeviceStatusChangeHistory {
    private final List<ProductDeviceStatusStatistics> statistics;
    private final List<ProductDeviceStatusChangeHistoryDetail> offlineDetails;
    private final List<ProductDeviceStatusChangeHistoryDetail> onlineDetails;
    private final List<ProductDeviceStatusChangeHistoryDetail> workingDetails;
    private final List<ProductDeviceStatusChangeHistoryDetail> alarmingDetails;

    public ProductDeviceStatusChangeHistory(List<ProductDeviceStatusStatistics> statistics, List<ProductDeviceStatusChangeHistoryDetail> offlineDetails, List<ProductDeviceStatusChangeHistoryDetail> onlineDetails, List<ProductDeviceStatusChangeHistoryDetail> workingDetails, List<ProductDeviceStatusChangeHistoryDetail> alarmingDetails) {
        this.statistics = statistics;
        this.offlineDetails = offlineDetails;
        this.onlineDetails = onlineDetails;
        this.workingDetails = workingDetails;
        this.alarmingDetails = alarmingDetails;
    }

    public static ProductDeviceStatusChangeHistory create(List<ProductDeviceStatusStatistics> statistics, List<ProductDeviceStatusChangeHistoryDetail> details) {
        if (null == details || details.isEmpty()) {
            return new ProductDeviceStatusChangeHistory(Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
        }
        Map<DeviceStatusEnum, List<ProductDeviceStatusChangeHistoryDetail>> detailMap = details.stream().collect(Collectors.groupingBy(ProductDeviceStatusChangeHistoryDetail::getStatus));
        return new ProductDeviceStatusChangeHistory(
                statistics,
                detailMap.getOrDefault(DeviceStatusEnum.OFFLINE, Collections.emptyList()),
                detailMap.getOrDefault(DeviceStatusEnum.ONLINE, Collections.emptyList()),
                detailMap.getOrDefault(DeviceStatusEnum.WORKING, Collections.emptyList()),
                detailMap.getOrDefault(DeviceStatusEnum.ALARMING, Collections.emptyList())
        );
    }

    public List<Long> getTs() {
        if (null == offlineDetails || offlineDetails.isEmpty()) {
            return Collections.emptyList();
        }
        List<Long> offlineTs = offlineDetails.stream().map(ProductDeviceStatusChangeHistoryDetail::getTs).toList();
        List<Long> onlineTs = onlineDetails.stream().map(ProductDeviceStatusChangeHistoryDetail::getTs).toList();
        List<Long> workingTs = workingDetails.stream().map(ProductDeviceStatusChangeHistoryDetail::getTs).toList();
        List<Long> alarmingTs = alarmingDetails.stream().map(ProductDeviceStatusChangeHistoryDetail::getTs).toList();
        List<Long> ts = new ArrayList<>();
        ts.addAll(offlineTs);
        ts.addAll(onlineTs);
        ts.addAll(workingTs);
        ts.addAll(alarmingTs);
        return ts.stream().distinct().sorted().toList();
    }
}
